package org.example.struct.array2;

import java.util.Arrays;

/**
 * @author liyishan
 * @date 2023/11/2 09:25
 * @apiNote
 */

public class Test01011ShipWithinDays {

    public static void main(String[] args) {
        int[] weights = {1, 2, 3, 1, 1};
        System.out.println(test(weights, 5));
    }

    public static int test(int[] weights, int days) {
        int left = Arrays.stream(weights).max().getAsInt();
        int right = Arrays.stream(weights).sum();
        while (left < right) {
            int mid = (left + right) / 2;
            int need = 1, cur = 0;
            for (int weight : weights) {
                if (cur + weight > mid) {
                    need++;
                    cur = 0;
                }
                cur += weight;
            }
            if (need <= days) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;

    }
}
